package com.sky.service.user.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sky.constant.BaseServiceConstant;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.exception.ParameterInvalidException;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.ShoppingCart;
import com.sky.service.user.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车业务实现
 *
 * @author zgp
 * @version V1.0
 * @date 2025-01-27 14:59
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public List<ShoppingCart> getShoppingCartList() {
        return shoppingCartMapper.selectByUserId(BaseContext.getCurrentId());
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void addItem(ShoppingCartDTO shoppingCartDTO) {
        // 校验参数
        if (ObjectUtil.isNull(shoppingCartDTO) ||
                (ObjectUtil.isNull(shoppingCartDTO.getDishId()) &&
                        ObjectUtil.isNull(shoppingCartDTO.getSetmealId()))) {
            throw new ParameterInvalidException(MessageConstant.SHOPPING_CART_ITEM_IS_NULL);
        }
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setDishId(dishId);
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        shoppingCart.setSetmealId(setmealId);
        // 1. 判断当前菜品或套餐是否是第一次添加 (根据用户id和菜品id及口味或套餐id)
        ShoppingCart shoppingCartItem = shoppingCartMapper.selectByUserIdAndDishIdOrSetmealId(shoppingCart);
        if (ObjectUtil.isNotNull(shoppingCartItem)) {
            // 2. 非首次添加，更新当前菜品或套餐数量
            shoppingCartMapper.updateAddNumberById(shoppingCartItem.getId());
            return;
        } else {
            // 3. 首次添加，判断当前添加购物车信息为菜品还是套餐
            shoppingCart.setNumber(BaseServiceConstant.ONE);
            shoppingCart.setCreateTime(LocalDateTime.now());
            if (ObjectUtil.isNotNull(dishId)) {
                // 3.1 添加菜品
                DishVO dishVO = dishMapper.selectById(dishId);
                if (ObjectUtil.isNull(dishVO)) {
                    throw new ParameterInvalidException(MessageConstant.SHOPPING_CART_DISH_ID_IS_INVALID);
                }
                // 补全菜品信息
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
            } else {
                // 3.2 添加套餐
                SetmealVO setmealVO = setmealMapper.selectById(setmealId);
                if (ObjectUtil.isNull(setmealVO)) {
                    throw new ParameterInvalidException(MessageConstant.SHOPPING_CART_SETMEAL_ID_IS_INVALID);
                }
                shoppingCart.setSetmealId(setmealId);
                // 补全套餐信息
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
            }
        }
        // 4. 插入购物车信息
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void cleanShoppingCartByUserId() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void deleteShoppingCartItem(ShoppingCartDTO shoppingCartDTO) {
        // 校验参数
        if (ObjectUtil.isNull(shoppingCartDTO) ||
                (ObjectUtil.isNull(shoppingCartDTO.getDishId()) &&
                        ObjectUtil.isNull(shoppingCartDTO.getSetmealId()))) {
            throw new ParameterInvalidException(MessageConstant.SHOPPING_CART_ITEM_IS_NULL);
        }
        // 处理删除业务
        // 1. 查询当前删除菜品或套餐份数
        ShoppingCart cartParam = new ShoppingCart();
        cartParam.setUserId(BaseContext.getCurrentId());
        cartParam.setDishId(shoppingCartDTO.getDishId());
        cartParam.setDishFlavor(shoppingCartDTO.getDishFlavor());
        cartParam.setSetmealId(shoppingCartDTO.getSetmealId());
        ShoppingCart shoppingCartItem = shoppingCartMapper.selectByUserIdAndDishIdOrSetmealId(cartParam);
        // 2. 判断需要直接删除还是更新份数
        if (ObjectUtil.isNotNull(shoppingCartItem) && shoppingCartItem.getNumber() > BaseServiceConstant.ONE) {
            // 2.1 当前份数 > 1，更新
            shoppingCartMapper.updateSubNumberById(shoppingCartItem.getId());
        } else {
            // 2.2 当前份数 = 1，删除
            shoppingCartMapper.deleteById(shoppingCartItem.getId());
        }
    }
}
